<?

/**
 * Delegate and menage dirvers to connect to database.
 * Delegate DBDriver class. For javadocs/phpdocs see DBDriver. 
 *  
 * @package SpotLight.core.db
 * @version since begin
 * @author asmhack
 * @license GPL
 * @copyright Serhiy Zaporozhets'
 */
class Connection {
	
	private static $instance = null;
	
	private $drv;
	
	private function __construct() {
		$this->drv = null;
		$this->getDriver ();
	}
	
	private function __clone() {
	}
	
	public static function &getInstance() {
		if (self::$instance == null)
			self::$instance = new Connection ( );
		return self::$instance;
	}
	
	private function isDriverNull() {
		if ($this->drv == null)
			die ( 'Driver is NULL' );
	}
	
	public function query($query) {
		$this->isDriverNull ();
		return $this->drv->query ( $query );
	}
	
	public function error() {
		$this->isDriverNull ();
		return $this->drv->error ( $query );
	}
	
	public function getQueryResult() {
		$this->isDriverNull ();
		return $this->drv->getRes ();
	}
	
	public function escapeString($str) {
		$this->isDriverNull ();
		return $this->drv->escapeString ( $str );
	}
	
	public function fetch_assoc() {
		$this->isDriverNull ();
		return $this->drv->fetch_assoc ();
	}
	
	public function fetch_object() {
		$this->isDriverNull ();
		return $this->drv->fetch_object ();
	}
	
	public function fetch_all() {
		$this->isDriverNull ();
		return $this->drv->fetch_all ();
	}
	
	public function affected_rows() {
		$this->isDriverNull ();
		return $this->drv->affected_rows ();
	}
	
	public function num_rows() {
		$this->isDriverNull ();
		return $this->drv->num_rows ();
	}
	
	public function close() {
		$this->isDriverNull ();
		$this->drv->close ();
	}
	
	public function getLink() {
		$this->isDriverNull ();
		return $this->drv->getLink ();
	}
	
	public function select_db($dbName) {
		$this->isDriverNull ();
		$this->drv->select_db ( $dbName );
	}
	
	public function last_id() {
		$this->isDriverNull ();
		return $this->drv->last_id ();
	}
	
	/**
	 * 
	 * Get instance of database driver
	 * @return unknown_type
	 */
	private function getDriver() {
		global $dbConfHost, $dbConfLogin, $dbConfPasswd, $dbConfDB, $dbDriverName, $dbEncoding;
		
		$fileName = 'SpotLight/core/db/drivers/' . $dbDriverName . '.class.php';
		
		if (file_exists ( $fileName ))
			require_once $fileName;
		else
			throw new DBException ( 'Driver file not found: <b>' . $fileName . '</b>' );
		
		if (! class_exists ( $dbDriverName ))
			throw new DBException ( 'Driver class <b>' . $dbDriverName . '</b> is not found in file <b>' . $fileName . '</b>' );
		
		$drvReflectionClass = new ReflectionClass ( $dbDriverName );
		
		$this->drv = $drvReflectionClass->newInstance ( $dbConfHost, $dbConfLogin, $dbConfPasswd, $dbConfDB, $dbEncoding );
		
		if (! is_subclass_of ( $this->drv, 'DBDriver' )) {
			$this->drv = null;
			throw new DBException ( 'Database Driver inheritance error!!!' );
		}
	}
	
	/*just test method*/
	function getInsertQuery($table, $row) {
		return "INSERT INTO `$table` (" . implode ( ",", array_map ( create_function ( '$a', 'return "`$a`";' ), array_keys ( $row ) ) ) . ") VALUES (" . implode ( ",", array_map ( create_function ( '$a', 'return "\"" . mysql_real_escape_string ($a) . "\"";' ), $row ) ) . ")";
	}

}

?>